package com.klhappy.houyuan.fragement.dingdanfragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.klhappy.houyuan.R;
import com.klhappy.houyuan.adapter.PayAdapter;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.bean.DingDanBean.PayBean;
import com.klhappy.houyuan.bean.DingDanBean.Records;
import com.klhappy.houyuan.bean.GoodsAllBean;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.dialog.NetWorkDialog;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.DingDanModle;
import com.klhappy.houyuan.utils.JsonCode;
import com.klhappy.houyuan.utils.NotifyMethod;
import com.klhappy.houyuan.view.AbsFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by MrPu on 15/12/12.
 */
public class PayFragment extends AbsFragment implements CallBackInterModle {
    //待付款
    public static final String CAG = "PayFragment";

    @InjectView(R.id.pay_lv)
    ListView pay_lv;

    private DingDanModle modle;
    private PayBean payBean;
    private List<Records> records;
    private PayAdapter adapter;
    private NetWorkDialog dialog;

    private ArrayList<GoodsAllBean> itemList;

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dingdan_pay, container, false);
        ButterKnife.inject(this, view);
        dialog = new NetWorkDialog(getActivity());
        modle = new DingDanModle(getActivity());
        modle.setCallBackInterModle(this);

        NotifyMethod.getInstance().addObserver(CAG, new NotifyMethod.NotifyListener() {
            @Override
            public void notify(Object o) {
                modle.setCancel((String) o);
                showDialog("取消中...");
            }
        });

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        modle.getPay();
    }

    @Override
    public void CallBackFromModle(String url, BaseJsonBean o) {
        if (url.equals(UrlConstant.ORDER_LIST_URL)){
            if (o.getCode() == 200){
                payBean = JsonCode.getClass(o.getEntity() ,PayBean.class);
                records = JsonCode.getClassList(payBean.getRecords(), Records.class);
                adapter = new PayAdapter(getActivity() ,records);
                pay_lv.setAdapter(adapter);
            }else{
                dialog.netWork(o.getMsg());
                dialog.show();
            }
        }else if (url.equals(UrlConstant.CANCEL_URL)){
            if (o.getCode() == 200){
                adapter.notifyDataSetChanged();
                showToast(o.getMsg());
            }else{
                dialog.netWork(o.getMsg());
                dialog.show();
            }
            closeDialog();
        }
    }
}
